<template>
  <el-dialog
      title="添加人员"
      v-model="showDialog"
      width="400px"
      @close="handleClose"
  >
    <el-form :model="author" label-width="80px">
      <el-form-item label="人名/账号">
        <el-select
            filterable
            allow-create
            v-model="author_name_account">
          <el-option v-for="item in author_list"
                     :key="item.username"
                     :label="`${item.name} (${item.username})`"
                     :value="JSON.stringify({name: item.name, username: item.username})">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="author.role">
          <el-option label="负责人" value="负责人"></el-option>
          <el-option label="成员" value="成员"></el-option>
          <el-option label="其他" value="其他"></el-option>
          <!-- 更多角色选项 -->
        </el-select>
      </el-form-item>
      <el-form-item label="分数比例"  >
        <el-input v-model="author.weight" placeholder="分多少比值的分给此人员(小数)"/>
      </el-form-item>
    </el-form>
<!--    添加一个提示信息有四行-->
    <div class="el-dialog__body">
      <span style="color: #999999;">
        负责人必须为第一人员否则会自动修改。<br/>
        负责人只能有一位，否则添加失败<br/>
        所有人员的分数比例之和必须为1<br/>
        分数比例为(0,1]区间内的数值<br/>
      </span>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue';
import eventBus from "@/event-bus.js";
import {ElMessage} from "element-plus";
const props = defineProps({
  Type: {
    type: String,
    required: true
  }
});
const showDialog = ref(false);
const author = ref({
  name: '',
  account: '',
  role: '',
  weight: null,
});
const author_name_account = ref(null);
const author_list = ref([]);


const open = (isadmin,in_authors) => {
  showDialog.value = true;
  const alluser =localStorage.getItem('alluser')
  author_list.value = JSON.parse(alluser);
  const userinfoString = localStorage.getItem('userinfo')
  const userinfo = JSON.parse(userinfoString);
  if (!isadmin && in_authors.length === 0) {
    author.value.name =userinfo['name'];
    author.value.account =userinfo['username'];
    author.value.role ='负责人';
    author.value.weight = null;
    author_name_account.value = JSON.stringify({
      name: userinfo['name'],
      username: userinfo['username']
    })
  }


};

const handleClose = () => {
  showDialog.value = false;
};

const confirm = () => {
  try {
    if (author.value.weight > 1) {
      ElMessage.error('分数比例不能超过1');
      return;
    } else if (author.value.weight === 0) {
      ElMessage.error('分数比例不能为0');
      return;
    }
    author_name_account.value = JSON.parse(author_name_account.value);
    author.value.account = author_name_account.value.username;
    author.value.name = author_name_account.value.name;
    author_name_account.value = null;
    if (props.Type === 'add') {
      eventBus.emit('author-add', {...author.value});
    } else if (props.Type === 'edit') {
      eventBus.emit('author-edit', {...author.value});
    }
    handleClose();
    author.value = {name: '', account: '', role: ''};
  } catch (e) {
    ElMessage.error('添加失败,请检查网络或刷新后重试：'+e);
  }
};

defineExpose({
  open,
});
</script>
